package com.hfdg.organization.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfdg.prototype.base.utils.TimeUtils;
import com.hfdg.organization.mapper.DepartmentMapper;
import com.hfdg.organization.entity.Department;
import com.hfdg.organization.service.DepartmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName DepartmentServiceImpl
 * @Description TODO
 * @Author yuelh
 * @Date 2019/1/19 18:26
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public String insert(Department department) {
        String uid = UUID.randomUUID().toString();
        department.setUid(uid);
        department.setDelFlag("0");
        department.setCreateTime(new Date());
        departmentMapper.insert(department);
        return uid;
    }

    @Override
    public Integer update(Department department) {
        department.setUpdateTime(new Date());
        return departmentMapper.update(department);
    }

    @Override
    public Integer  delete(String uid) {
        return departmentMapper.delete(uid);
    }
    @Override
    public Integer batchDelete(String[] uids) {
            return departmentMapper.batchDelete(uids);
    }

    @Override
    public Department get(String uid) {
        return departmentMapper.get(uid);
    }

    @Override
    public List<Department> list(Department department) {
    return  departmentMapper.list(department);
    }

    @Override
    public List<Map<String, Object>> listDepartmentTree() {
        return departmentMapper.listDepartmentTree();
    }

    @Override
    public PageInfo<Department> getPagingData(int offset, int limit,String orderBy, Department department) {
        if(null==orderBy|| StringUtils.isBlank(orderBy)){
        PageHelper.startPage((offset/limit)+1,limit);
        }else{
        PageHelper.startPage((offset/limit)+1,limit,orderBy);
        }
        PageInfo<Department> pageInfo =new PageInfo<>(departmentMapper.list(department));
            return pageInfo;
            }
    @Override
    public String insertOrUpdate(Department department){
        if(null!=department.getUid()){
            this.update(department);
        }else{
            this.insert(department);
        }
        return department.getUid();
    }
            }
